package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.suggest.SuggestAddRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestAnswerRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestSearchRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestUpdateRequest;
import com.lnking.lnkingplace.service.ISuggestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/suggest")
@Api(tags = "建议信箱")
public class SuggestController {

    @Resource
    ISuggestService suggestService;

    @Resource
    UserUtil userUtil;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ReturnResult getList(@RequestBody SuggestSearchRequest params) {
        params.setChannel(userUtil.getUser().getChannel());
        //人员类型
        String userType = userUtil.getUser().getType();
        //从业人员根据userId查询
        if(userType.equals(UserType.USER)){
            params.setUserId(userUtil.getUser().getId());
        }
        //场所人员按placeId查询
        else if(userType.equals(UserType.LOCATION)){
            params.setPlaceId(userUtil.getUser().getPlaceId());
        }
        return ReturnResult.success(suggestService.pageList(params));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public ReturnResult add(@RequestBody @Valid SuggestAddRequest params)throws Exception{
        suggestService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ReturnResult update(@RequestBody @Valid SuggestUpdateRequest params)throws Exception{
        suggestService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        suggestService.removeById(id);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(suggestService.getById(id));
    }

    @ApiOperation("监管商回复")
    @PostMapping("answer")
    public ReturnResult answer(@RequestBody SuggestAnswerRequest params){
        suggestService.updateAnswer(params.getId(),params.getAnswer());
        return ReturnResult.success();
    }
}
